package com.hfjyz.tutor.tutorcall.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.tutor.tutorcall.bean.TutorCallBean;
import com.hfjyz.tutor.tutorcall.bean.TutorCallExcelBean;
import com.hfjyz.tutor.tutorcall.bean.TutorCallSearchBean;

public class TutorCallExcelAction extends TutorCallAction {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -7168323322187048129L;
    private static final String XLS_TUTOR_TEMPLATE = "tutorCallTemplate.xls";

    private String downloadFileName = null;
    private InputStream excelStream = null;

    public String getDownloadFileName() {
        return downloadFileName;
    }

    public void setDownloadFileName(String downloadFileName) {
        this.downloadFileName = downloadFileName;
    }

    public InputStream getExcelStream() {
        return excelStream;
    }

    public void setExcelStream(InputStream excelStream) {
        this.excelStream = excelStream;
    }

    public String exportExcel() {
        ByteArrayOutputStream baos = null;
        try {
            String rootPath = ServletActionContext.getServletContext().getRealPath("/");
            String templatePath = rootPath + "WEB-INF\\conf\\tutor\\" + XLS_TUTOR_TEMPLATE;

            HttpSession session = ServletActionContext.getRequest().getSession();
            Object o = session.getAttribute(BaseConstant.SESSION_CONDITION_KEY);
            TutorCallSearchBean searchBean = null;
            if (o instanceof TutorCallSearchBean) {
                searchBean = (TutorCallSearchBean) o;
            }
            if (searchBean == null) {
                searchBean = tutorCallSearch;
            }
            List<TutorCallBean> tutorCallList = getService().findTutorCall(getSearchCondition(), searchBean);
            List<TutorCallExcelBean> excelList = getExcelBeanList(tutorCallList);

            Workbook wb = getService().exportTutorCall(templatePath, excelList);
            baos = new ByteArrayOutputStream();
            wb.write(baos);
            baos.flush();
            byte[] excelBytes = baos.toByteArray();
            excelStream = new ByteArrayInputStream(excelBytes, 0, excelBytes.length);

            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            downloadFileName = "老师调用详细_" + sf.format(new Date()) + ".xls";
            downloadFileName = new String(downloadFileName.getBytes(), "ISO8859-1");
        } catch (Exception e) {
            map.put("errorMsg", e.getMessage());
        }
        return SUCCESS;
    }
    
    private List<TutorCallExcelBean> getExcelBeanList(List<TutorCallBean> tutorCallList) {
        List<TutorCallExcelBean> beanList = new ArrayList<TutorCallExcelBean>();
        if (tutorCallList == null || tutorCallList.isEmpty()) {
            return beanList;
        }
        
        for (TutorCallBean tutorCall : tutorCallList) {
            beanList.add(new TutorCallExcelBean(tutorCall));
        }
        return beanList;
    }
}
